//
//  LLCreateRemarkViewController.swift
//  LiLi
//
//  Created by liuzhu on 2019/1/24.
//  Copyright © 2019 liuzhu. All rights reserved.
//

import UIKit

class LLCreateRemarkViewController: UIViewController {

    public var action:LLAction = LLAction()
    override func viewDidLoad() {
        super.viewDidLoad()
                navigationItem.rightBarButtonItem = UIBarButtonItem(title: "完成", style: UIBarButtonItem.Style.plain, target: self, action: #selector(done))
        self.title = "新建日记"
        self.view.addSubview(textView)
    }
    
    @objc func done(){
        if textView.text.count <= 0{
            SVProgressHUD.showError(withStatus: "日记内容不能为空")
            return
        }
        let remark = LLRemark()
        let randNum = String(format: "%d", arc4random() % 100000)
        let str = action.recordName!
        remark.recordName = "\(str)-\(randNum)"
        remark.refreshTag = 0
        remark.des = textView.text
        remark.titleName = ""
        remark.actionId = action.recordName
        remark.imageId = ""
        let date = LLDateUtil.shareLLDateUtil().getCurrentDate()
        let a = date.timeIntervalSince1970
        print(a)
        remark.createTime = String.init(format: "%.f", a)
        DataManager.saveRemark(remark: remark) { (isSuccess) in
            if(isSuccess){
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: LLRemarkAddKey), object: nil, userInfo: ["remark":remark])
                self.navigationController?.popViewController(animated: false)
            }else{
                SVProgressHUD.showError(withStatus: "新建日记失败")
                self.navigationController?.popViewController(animated: false)
            }
        }
    }
    
    private lazy var textView:UITextView = {
        var y = CGFloat(64)
        if LLConst.getPhoneType() == PhoneType.PHONE_MAX_XR || LLConst.getPhoneType() == PhoneType.PHONE_X {
            y = 88
        }
        let tv = UITextView.init(frame: CGRect(x: 0, y: y, width: UIScreen.main.bounds.size.width, height: (UIScreen.main.bounds.size.height - y)/2), textContainer: nil)
        tv.becomeFirstResponder()
        return tv
    }()
}
